/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.document.read.pdf;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.pdmodel.PDPage;
import org.dopdf.document.model.ResourceDetail;
import org.dopdf.io.Image;
import org.dopdf.utils.Constants;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The class that represents a pdf page.
 *
 * @author Deep Shah
 */
public class PDFPage {

    /** The java util logger reference. */
    private static final Logger logger = Logger.getLogger(PDFPage.class.toString());

    /** The PDPage reference. */
    private PDPage page;

    /**
     * The constructor that takes in a PDPage reference as argument.  The
     * @param page the pd page refernce that is represented by this pdf page.
     */
    public PDFPage(final PDPage page) {
        this.page = page;
    }

    /**
     * Returns the page as image
     * @param size The size of image to render.
     * @return the ResourceDetail reference that represents the converted image.
     */
    public ResourceDetail asImage(final Constants.PageSize size) {
        try {
            return new Image(page.convertToImage()).scale(size);
        } catch (final Exception e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
            throw new IllegalStateException(e);
        }
    }
}
